<?php
/**
 * User: hufeng
 * Date: 2016/12/13
 * Desc: 后台服务基类
 */
namespace Admin\Controller;
use Think\Controller;
use Model\AdminGroupModel;
use Model\AdminMenuModel;
use Model\AdminLogModel;

class BaseController extends Controller{
    public $adminUserData;
    public $domianUrl;
    public $pernum = 8;//每页显示条数
    public $logModel;
    public $haveIndexBool=1;//是否有首页权限
    public $dateNow;
    public function __construct(){
        parent::__construct();
        $this->domianUrl = C('DOMAIN_URL');
        $this->adminUserData = session('JSADMIN_USERDATA');
        $this->dateNow = date('Y-m-d H:i:s');
        if (!$this->adminUserData) {
            $this->redirect('/login');
        }

        //判断当前菜单高亮
        $currentActionStr = lcfirst(str_ireplace('/Admin/','',__ACTION__));
        $leftMenu = $this->userMenu();

        $havePower = 0;
        foreach($leftMenu as $key=>$val){
            if(strtolower($currentActionStr)==strtolower($val['url'])){
                $leftMenu[$key]['active'] = 1;
            }
            foreach($val['child'] as $ckey=>$cval){
                if(strtolower($currentActionStr)==strtolower($cval['url'])){
                    $leftMenu[$key]['active'] = 1;
                    $leftMenu[$key]['child'][$ckey]['active'] = 1;
                }
                if($cval['ismenu'] == 2 && strtolower($currentActionStr)==strtolower($cval['url'])){
                    foreach($val['child'] as $dkey=>$dval){
                        if(strtolower($dval['url'])==strtolower($cval['purl'])){
                            $leftMenu[$key]['active'] = 1;
                            $leftMenu[$key]['child'][$dkey]['active'] = 1;
                        }
                    }
                }
                if(strtolower($currentActionStr)==strtolower($cval['url'])){
                    $havePower = 1;
                }
            }
            if(strtolower($currentActionStr)==strtolower($val['url'])){
                $havePower = 1;
            }
        }
        //没有权限的话，跳转到首页
        if($havePower == 0){
            //如果当前是首页
            if(strtolower($currentActionStr)=='index/index'){
                $this->haveIndexBool = 0;//没有首页权限，只展示工作台
            }else{
                if($_POST){
                    $this->printOut(['code'=>1,'msg'=>'抱歉您没有该项操作权限']);
                }else{
                    redirect($this->domianUrl);
                }
            }
        }
        $this->logModel = new AdminLogModel();
        $this->assign('leftMenu',$leftMenu);
    }

    //检测用户的菜单权限
    public function userMenu(){
        if(session('userMenuArr')){
            return session('userMenuArr');
        }
        $gids = rtrim($this->adminUserData['gids'],',');
        $gidArr = explode(',',$gids);
        if(!$gidArr){
            return false;
        }
        $agModel = new AdminGroupModel();
        $agList = $agModel->selectData(['id'=>['in',$gidArr],'status'=>1]);

        $midArr = [];
        foreach($agList as $val){
            $menu_ids = rtrim($val['menu_ids'],',');
            $arr = explode(',',$menu_ids);
            $midArr += $arr;
        }
        $midArr = array_unique($midArr);
        if(!$midArr){
            return false;
        }

        $lmClass = C('LEFTMENU_CLASS');
        $amModel = new AdminMenuModel();
        $amList = $amModel->search(['id'=>['in',$midArr],'status'=>1,'platform'=>1],0,0,'pid asc',true);
        $amData = [];
        foreach($amList as $val){
            $arr = [];
            $arr['id'] = $val['id'];
            $arr['name'] = $val['name'];
            $arr['url'] = $val['url'];
            $arr['ismenu'] = $val['ismenu'];
            $arr['purl'] = $val['purl'];
            $arr['class'] = '';
            if($lmClass[$val['id']]){
                $arr['class'] = $lmClass[$val['id']];
            }
            if($val['pid']==0){
                $amData[$val['id']]=$arr;
            }else{
                $amData[$val['pid']]['child'][] = $arr;
            }
        }
        session('userMenuArr',$amData);
        return $amData;
    }
    //输出json结果
    protected function printOut($data){
        header('Content-type: application/json');
        echo json_encode($data);exit;
    }
}